package com.bzkj.basicinfo.service.impl.ncc;

import com.bzkj.basicinfo.domain.basic.BasicStoArea;
import com.bzkj.basicinfo.domain.basic.vo.TreeSkuTypeVo;
import com.bzkj.basicinfo.domain.ncc.BasicSkuTypeDetail;
import com.bzkj.basicinfo.domain.ncc.NccBasicSkuType;
import com.bzkj.basicinfo.mapper.BasicSkuTypeDetailMapper;
import com.bzkj.basicinfo.mapper.BasicStoAreaMapper;
import com.bzkj.basicinfo.mapper.NccBasicSkuMapper;
import com.bzkj.basicinfo.mapper.NccBasicSkuTypeMapper;
import com.bzkj.basicinfo.service.ncc.BasicSkuTypeDetailService;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.R;
import com.bzkj.common.core.domain.entity.SysUser;
import com.bzkj.common.core.domain.model.LoginUser;
import com.bzkj.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service
public class BasicSkuTypeDetailServiceImpl implements BasicSkuTypeDetailService {
    @Autowired
    private BasicSkuTypeDetailMapper basicSkuTypeDetailMapper;
    @Autowired
    private NccBasicSkuMapper nccBasicSkuMapper;
    @Autowired
    private NccBasicSkuTypeMapper nccBasicSkuTypeMapper;
    @Autowired
    private BasicStoAreaMapper basicStoAreaMapper;

    @Override
    public AjaxResult getSkuTypeDetail(String id) {
        List<BasicSkuTypeDetail> skuTypeDetail = basicSkuTypeDetailMapper.getSkuTypeDetail(id);
        return AjaxResult.success(skuTypeDetail);
    }

    @Override
    public List<BasicSkuTypeDetail> getSkuTypeDetailAll() {
        List<BasicSkuTypeDetail> skuTypeDetailAll = basicSkuTypeDetailMapper.getSkuTypeDetailAll();
        return skuTypeDetailAll;
    }

    @Override
    public AjaxResult addSkuTypeDetail(String pk_marbasclass1, List<String> names) {
        //根据pk_marbasclass1去ncc_basic_sku_type表中查询出来ncc_stu_name还有pk_marbasclass
        //然后循环names，获取到每个name后，往basic_sku_type_detail_copy1表中添加数据
        Date date = new Date();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        String userName = user.getUserName();
        NccBasicSkuType nccBasicSkuType = nccBasicSkuMapper.queryNccBasicSkuTypeByName(pk_marbasclass1);
        if (nccBasicSkuType == null) {
            return AjaxResult.error("当前商品分类不存在");
        }
        String pk_marbasclass = nccBasicSkuType.getPkMarbasclass();


        for (String s : names) {
            String ss = getFive();

            //根据stu_class_name和parent_class_id去basic_sku_type_detail_copy1判断，是否有一样的，没有再新增，有就不做处理
            List<BasicSkuTypeDetail> basicSkuTypeDetails = basicSkuTypeDetailMapper.selectByIdAndName(pk_marbasclass, s);
            if (basicSkuTypeDetails.isEmpty()) {
                BasicSkuTypeDetail basicSkuTypeDetail = new BasicSkuTypeDetail();
                basicSkuTypeDetail.setStuClassCode(ss);
                basicSkuTypeDetail.setStuClassName(s);

                basicSkuTypeDetail.setStuClassId(ss);
                basicSkuTypeDetail.setParentClassId(pk_marbasclass);
                basicSkuTypeDetail.setCreatetime(date);
                basicSkuTypeDetail.setEnableState("1");
                basicSkuTypeDetail.setCreator(userName);
                basicSkuTypeDetailMapper.insertBasicSkuTypeDetail(basicSkuTypeDetail);
            }
        }
        return AjaxResult.success();
    }

    @Override
    public int deleteSkuTypeDetail(String id) {
        return basicSkuTypeDetailMapper.deleteSkuTypeDetail(id);
    }

    @Override
    public int updateSkuTypeDetail(BasicSkuTypeDetail basicSkuTypeDetail) {
        basicSkuTypeDetail.setTstime(new Date());
        return basicSkuTypeDetailMapper.updateSkuTypeDetail(basicSkuTypeDetail);
    }

    @Override
    public R findTreeSkuTypeData() {
        List<NccBasicSkuType> list = nccBasicSkuTypeMapper.getSkuTypeAll();
        List<BasicSkuTypeDetail> all = basicSkuTypeDetailMapper.getSkuTypeDetailAll();
        Map<String, TreeSkuTypeVo> map = new HashMap<>();
        List<TreeSkuTypeVo> collect = list.stream().map(item -> {
            TreeSkuTypeVo temp = new TreeSkuTypeVo();
            BeanUtils.copyProperties(item, temp);
            map.put(temp.getPkMarbasclass(), temp);
            return temp;
        }).collect(Collectors.toList());

        all.forEach(item -> {
            TreeSkuTypeVo type = map.get(item.getParentClassId());
            if (!Objects.isNull(type)) {
                type.setChildrenAdd(item);
            }
        });

        return R.ok(collect);
    }

    @Override
    public List<BasicStoArea> findBasicSkuTypeDetailByClassId(BasicSkuTypeDetail entity)
    {
        List<BasicStoArea> list = basicStoAreaMapper.findStoAreaTree(entity);
        return list;
    }

    @Override
    public AjaxResult basicSkuTypeDetailSave(BasicSkuTypeDetail entity) {
        //根据pk_marbasclass1去ncc_basic_sku_type表中查询出来ncc_stu_name还有pk_marbasclass
        //然后循环names，获取到每个name后，往basic_sku_type_detail_copy1表中添加数据
        Date date = new Date();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        String userName = user.getUserName();
        NccBasicSkuType nccBasicSkuType = nccBasicSkuMapper.getOne(entity.getPk_marbasclass());
        if (nccBasicSkuType == null) {
            return AjaxResult.error("当前商品分类不存在");
        }
        String pk_marbasclass = entity.getPk_marbasclass();

        String ss = getFive();

        //根据stu_class_name和parent_class_id去basic_sku_type_detail_copy1判断，是否有一样的，没有再新增，有就不做处理
        List<BasicSkuTypeDetail> basicSkuTypeDetails = basicSkuTypeDetailMapper.selectByIdAndName(pk_marbasclass, entity.getStuClassName());
        if (basicSkuTypeDetails.isEmpty()) {
            BasicSkuTypeDetail basicSkuTypeDetail = new BasicSkuTypeDetail();
            basicSkuTypeDetail.setStuClassCode(ss);
            basicSkuTypeDetail.setStuClassName(entity.getStuClassName());

            basicSkuTypeDetail.setStuClassId(ss);
            basicSkuTypeDetail.setParentClassId(pk_marbasclass);
            basicSkuTypeDetail.setCreatetime(date);
            basicSkuTypeDetail.setEnableState("1");
            basicSkuTypeDetail.setCreator(userName);
            basicSkuTypeDetailMapper.insertBasicSkuTypeDetail(basicSkuTypeDetail);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateSkuTypeStatus(String id, String status) {
        basicSkuTypeDetailMapper.updateSkuTypeStatus(id, status);
        return AjaxResult.success("修改成功");
    }

    public String getFive() {
        Long aLong = basicSkuTypeDetailMapper.queryBigNumber();
        if (aLong == null) {
            return "5000000000";
        } else {
            aLong = aLong + 1;
            return aLong + "";
        }
    }

}
